Skip to content

Comments

Wrap writing events in a lock to avoid race condition#3

Merged
prochnowc merged 1 commit intoAppCoreNet:mainfrom
sdc-ad:features/lock_event_write
Dec 20, 2024
Merged

Wrap writing events in a lock to avoid race condition#3
prochnowc merged 1 commit intoAppCoreNet:mainfrom
sdc-ad:features/lock_event_write

Conversation

@sdc-ad
Copy link
Contributor

@sdc-ad sdc-ad commented Dec 19, 2024

If multiple threads, or multiple instance of the application attempt to write events to the event store at the same time, it can cause some events to be skipped. For example:

Thread Action
Writer 1 Starts Transaction
Writer 1 Writes Event and updates the EventStream table with the new ID
Writer 2 Starts Transaction
Writer 2 Writes Event and updates the EventStream table with the new ID
(this will be higher than the ID that Writer 1 wrote)
Writer 2 Commits Transaction
Reader Reads Event committed by Writer 2
Reader Updates EventSubscription with processed ID
Writer 1 Commits Transaction

The event from Writer 1 will never be processed as its ID is lower than the one that was already processed from Writer 2.

Therefore, we need to ensure that the events are committed in the same order as the IDs are allocated. We cannot perform this locking on the .NET side as there may be multiple instances of the application.

@prochnowc prochnowc merged commit bec0362 into AppCoreNet:main Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants